<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2020-04-25 21:18:54
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-12 15:11:24
 -->
<template>
  <div>
    <el-card shadow="never" class="oper-btn-card">
      <div class="clearfix">
        <el-form
          class="fl form-no-bottom"
          :inline="true"
          :model="searchData"
          label-position="left"
          label-width="80px"
          @keydown.enter.native="btnSearch"
        >
          <el-form-item>
            <el-input
              v-model="searchData.keywords"
              clearable
              style="width: 300px"
              placeholder="按单据号,商品名,供应商,备注"
            />
            <el-button type="primary" :loading="btnLoading.search" @click="btnSearch()">
              搜索
            </el-button>
            <el-button type="default" @click="isFilterOpen = !isFilterOpen">
              {{ btnFilterOpenText }}
            </el-button>
          </el-form-item>
        </el-form>
        <div class="fr">
          <el-button type="default" @click="btnAddData">导出</el-button>
          <el-button type="primary" @click="btnAddData">批量入库</el-button>
        </div>
      </div>
      <SearchFilter
        v-show="isFilterOpen"
        :search="btnFilterSearch"
        :clear="btnClearConditions"
      >
        <Conditions ref="conditions" :query-data="searchData" />
      </SearchFilter>
    </el-card>
    <div class="margin-t2">
      <vxe-grid
        ref="table"
        class="grid"
        border
        resizable
        highlight-hover-row
        :loading="tableLoading.table"
        :columns="columnsWaiteInData"
        :data="tableData.table"
      />
    </div>
    <div class="clearfix">
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="pageInfo.rows"
          :page-sizes="[20,30,40]"
          :current-page.sync="pageInfo.page"
          :total="pageInfo.total"
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { getWaiteStockInBillList } from '@/api/bill/bill-stock-in.js'
import SearchFilter from '@/views/components/SearchFilter'
import Conditions from './conditions'
import clonedeep from 'clonedeep'
import { getMoreButton, getTransButton } from '@/components/Table/oper-btns.js'
import { getBusinessTime } from '@/libs/util.js'

export default {
  components: { SearchFilter, Conditions },
  mixins: [oftenuse],
  data() {
    return {
      searchData: { stockStatus: '-1', dateTab: 3 },
      optionsData: { },
      currentIndex: 0,
      isFilterOpen: false,
      columnsWaiteInData: [
        { title: '操作', width: 80,
          slots: {
            default: (params) => {
              const { row } = params
              const h = this.$createElement
              const viewMoreNode = getMoreButton(h, { on: { click: () => { this.btnViewData(row) } }})
              const transBtnNode = getTransButton(h, {
                props: { type: 'primary' },
                attrs: { title: '入库' },
                on: { click: () => { this.btnTransData(row) } }
              })
              const divNode = h('div', {
                attrs: {
                  class: 'table-oper-btns'
                }
              }, [viewMoreNode, transBtnNode])
              return [divNode]
            }
          }
        },
        { field: 'billNo', title: '单据编号', width: 150 },
        { field: 'businessTime', title: '业务日期', width: 100,
          slots: {
            default: (params) => {
              const { row, column } = params
              return [getBusinessTime(row[column.property])]
            }
          }
        },
        { field: 'billCatName', title: '类型', width: 100 },
        { field: 'comegoName', title: '往来单位', width: 200, showOverflow: true },
        { field: 'goodsNamestr', title: '入库商品', width: 200, showOverflow: true },
        { field: 'goodsQuantity', title: '待入库量', width: 150,
          slots: {
            default: (params) => {
              const { row } = params
              const { goodsQuantity, changeQuantity } = row
              const value = this.$amount(goodsQuantity).subtract(changeQuantity).format()
              return [<span>{value}</span>]
            }
          }}
      ]
    }
  },
  computed: {
    'btnFilterOpenText': function() {
      return this.isFilterOpen ? '收起筛选' : '展开筛选'
    }
  },
  created() {
    this.listData()
  },
  methods: {
    btnAddData() {
      this.$router.push({
        name: 'PurchaseAdd'
      })
    },
    btnTransData(row) {
      const { id, billCat } = row
      this.$router.push({
        name: 'StockinAdd',
        query: { billId: id, billCat }
      })
    },
    btnSelectData(row) {
      this.$emit('on-select', clonedeep(row))
    },
    btnCopyAddData(row) {
      const { id } = row
      const params = { billId: id }
      this.$router.push({
        name: 'PurchasereturnCopyAdd',
        query: params
      })
    },
    btnViewData(row) {
      const { id } = row
      const params = { billId: id }
      this.$router.push({
        name: 'StockWaiteinDetail',
        query: params
      })
    },
    btnSearch() {
      this.setBtnLoading('search', true)
      this.pageInfo.current = 1
      this.listData().then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        console.log(err)
        this.setBtnLoading('search', false)
      })
    },
    btnDetailData(index, row) {
      const purchaseId = row.id
      this.jump2purchaseDetail({ purchaseId, operation: 'update' })
    },
    btnClearConditions() {
      this.$refs.conditions.clearData()
    },
    btnFilterSearch() {
      this.setBtnLoading('search', true)
      this.pageInfo.current = 1
      const searchData = this.$refs.conditions.searchData
      const params = {
        data: searchData,
        page: this.pageInfo
      }
      this.listData(params).then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        this.$message.error('查询异常:', err.message)
        this.setBtnLoading('search', false)
      })
    },
    // api
    listData(params = { data: this.searchData, page: this.pageInfo }) {
      if (params.data.date) {
        const data = params.data
        data.beginDate = data.date[0]
        data.endDate = data.date[1]
      }
      this.setTableLoading('table', true)
      return new Promise((resolve, reject) => {
        getWaiteStockInBillList(params).then(res => {
          this.setTableLoading('table', false)
          if (!res.data.flag) return
          const { records, total } = res.data.data
          this.setTableData('table', records)
          this.pageInfo.total = total
          resolve(res)
        }).catch(err => {
          reject(err)
          this.setTableLoading('table', false)
        })
      })
    },
    // on
    onSizeChange(val) {
      this.pageInfo.pagx1e = 1
      this.pageInfo.size = val
      this.listData()
    },
    onCurrentChange(val) {
      this.pageInfo.current = val
      this.listData()
    }
  }
}
</script>
